package com.ozo.easy.gen.util;

import cn.hutool.extra.spring.SpringUtil;
import com.ozo.easy.common.config.OzoProperties;
import com.ozo.easy.common.constant.BaseEnum;
import com.ozo.easy.common.constant.BaseFieldConstant;
import com.ozo.easy.common.util.NamingConventionUtil;
import com.ozo.easy.gen.constant.HtmlType;
import com.ozo.easy.gen.constant.MySQLColumnType;
import com.ozo.easy.gen.constant.PageFormType;
import com.ozo.easy.gen.constant.PageTableType;
import com.ozo.easy.gen.entity.GenTable;
import com.ozo.easy.gen.entity.GenTableColumn;
import com.ozo.easy.gen.pojo.bo.GenTableBO;
import com.ozo.easy.gen.pojo.bo.GenTableColumnBO;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 代码生成工具类
 *
 * @author ozo
 * @date 2025-01-06
 */
public class TableColumnInitUtil {

    /**
     * 下划线
     */
    public static final char UNDERLINE = '_';

    /**
     * 初始化表
     */
    public static GenTable initTable(GenTableBO tableBO) {
        String tableName = tableBO.getTableName();

        GenTable table = new GenTable();
        table.setTableName(tableName);
        table.setTableComment(tableBO.getTableComment());
        table.setGenClass(NamingConventionUtil.underlineToCamel(tableName, false));
        table.setGenFunction(tableBO.getTableComment());
        table.setGenOperateLog(true);
        table.setGenAdd(true);
        table.setGenRemove(true);
        table.setGenEdit(true);
        table.setGenSelect(true);
        table.setGenPage(true);
        table.setGenPageTableType(PageTableType.PAGE);
        table.setGenPageFormType(PageFormType.ONE);
        table.setRemark(null);

        // 项目默认配置
        OzoProperties ozoProperties = SpringUtil.getBean(OzoProperties.class);
        OzoProperties.GenConfig genConfig = ozoProperties.getGenConfig();
        table.setProjectName(genConfig.getProjectName());
        table.setGenAuthor(genConfig.getAuthor());
        table.setGenVersion(genConfig.getVersion());
        table.setGenPackage(genConfig.getGenPackage());
        table.setGenPath(genConfig.getProjectPath());
        return table;
    }

    /**
     * 初始化表字段
     */
    public static List<GenTableColumn> initTableColumn(Long tableId, List<GenTableColumnBO> columnBOList) {
        List<GenTableColumn> tableColumnList = new ArrayList<>();
        for (int i = 0; i < columnBOList.size(); i++) {
            GenTableColumnBO columnBO = columnBOList.get(i);

            GenTableColumn tableColumn = new GenTableColumn();
            tableColumn.setTableId(tableId);
            // 字段信息
            tableColumn.setColumnName(columnBO.getColumnName());
            tableColumn.setColumnComment(columnBO.getColumnComment());
            tableColumn.setColumnType(columnBO.getColumnType());
            tableColumn.setColumnPk(columnBO.getColumnPk());
            tableColumn.setColumnNotNull(columnBO.getColumnNotNull());
            tableColumn.setColumnIncrement(columnBO.getColumnIncrement() != null && columnBO.getColumnIncrement());
            tableColumn.setDataLength(columnBO.getDataLength());
            tableColumn.setDecimalLength(columnBO.getDecimalLength());
            // java类型信息
            tableColumn.setJavaField(NamingConventionUtil.underlineToCamel(columnBO.getColumnName(), true));
            tableColumn.setPageDictType(null);
            MySQLColumnType sqlColumnType = BaseEnum.getInstanceByDescNoe(MySQLColumnType.class, columnBO.getColumnType());
            if (sqlColumnType != null) {
                tableColumn.setJavaType(sqlColumnType.getJavaType().getDesc());
                tableColumn.setJavaTypePackage(sqlColumnType.getJavaType().getTypePackage());
                tableColumn.setPageHtmlType(sqlColumnType.getHtmlType());
            } else {
                tableColumn.setJavaType("String");
                tableColumn.setJavaTypePackage("java.lang.String");
                tableColumn.setPageHtmlType(HtmlType.INPUT);
            }
            // 页面信息
            tableColumn.setPageFormRequired(!columnBO.getColumnPk() && columnBO.getColumnNotNull());
            tableColumn.setPageForm(!columnBO.getColumnPk() && !arraysContains(BaseFieldConstant.BASE_COLUMN, columnBO.getColumnName()));
            tableColumn.setPageTable(tableColumn.getPageForm());
            tableColumn.setPageTableWidth(120);
            tableColumn.setPageTableQuery(false);
            tableColumn.setPageTableQueryType(null);
            tableColumn.setPageTableOrder(false);
            tableColumn.setOrderNum(i + 1);

            tableColumnList.add(tableColumn);
        }
        return tableColumnList;
    }

    /**
     * 校验数组是否包含指定值
     *
     * @param array
     * @param value
     * @return
     */
    public static boolean arraysContains(String[] array, String value) {
        return Arrays.asList(array).contains(value);
    }

}
